<template>
  <div class="message-list">
    <div
        v-for="message in messages"
        :key="message.id"
        class="message-item"
        :class="{
        'sent': message.sender === currentUserId,
        'received': message.sender !== currentUserId,
        'sending': message.status === 'sending',
        'failed': message.status === 'failed'
      }"
    >
      <div class="message-content">
        <div v-if="message.sender !== currentUserId" class="sender-info">
          <el-avatar :size="24" :src="getAvatar(message.sender)" />
          <span class="sender-name">{{ getSenderName(message.sender) }}</span>
        </div>
        <div class="message-bubble">
          <div class="message-text">{{ message.content }}</div>
          <div class="message-meta">
            <span class="timestamp">{{ formatTime(message.timestamp) }}</span>
            <span v-if="message.status === 'sending'" class="sending-indicator">
              <i class="el-icon-loading"></i>
            </span>
            <span v-if="message.status === 'failed'" class="failed-indicator">
              <i class="el-icon-warning"></i>
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { defineComponent } from 'vue'
import dayjs from 'dayjs'

export default defineComponent({
  props: {
    messages: {
      type: Array,
      default: () => []
    },
    currentUserId: {
      type: String,
      default: ''
    }
  },
  methods: {
    formatTime(timestamp) {
      return dayjs(timestamp).format('HH:mm')
    },
    getSenderName(senderId) {
      // 实际应用中应从联系人列表中获取
      return senderId === 'system' ? '系统通知' : '其他用户'
    },
    getAvatar(senderId) {
      // 实际应用中应从联系人列表中获取
      return 'https://via.placeholder.com/40'
    }
  }
})
</script>

<style scoped>
.message-list {
  padding: 20px 0;
}

.message-item {
  margin-bottom: 20px;
  display: flex;
}

.message-item.sent {
  justify-content: flex-end;
}

.message-content {
  max-width: 70%;
}

.sender-info {
  display: flex;
  align-items: center;
  margin-bottom: 5px;
}

.sender-name {
  margin-left: 8px;
  font-size: 12px;
  color: #666;
}

.message-bubble {
  padding: 10px 15px;
  border-radius: 5px;
  position: relative;
}

.sent .message-bubble {
  background-color: #95ec69;
  border-top-right-radius: 0;
}

.received .message-bubble {
  background-color: #ffffff;
  border-top-left-radius: 0;
}

.message-text {
  word-wrap: break-word;
  line-height: 1.5;
}

.message-meta {
  display: flex;
  justify-content: flex-end;
  margin-top: 5px;
  font-size: 12px;
  color: #999;
}

.timestamp {
  margin-right: 5px;
}

.sending-indicator,
.failed-indicator {
  color: #f56c6c;
}

.message-item.sending .message-bubble {
  opacity: 0.8;
}

.message-item.failed .message-bubble {
  background-color: #ffdddd;
}
</style>